package test5;


import java.util.Iterator;
import java.util.LinkedList;

public class ShoppingCart {
    private LinkedList<GoodsItem> goodsItems;
    private double account;

    public ShoppingCart() {
        goodsItems = new LinkedList<>();
    }

    public ShoppingCart(LinkedList<GoodsItem> goodsItems, double account) {
        this.goodsItems = goodsItems;
        this.account = account;
    }

    public void addGoods(GoodsItem goods) {
        goodsItems.add(goods);
    }

    public void addGoods(int goodsCode, String goodsName, double goodsPrice) {
        goodsItems.add(new GoodsItem(goodsCode, goodsName, goodsPrice));
    }

    public double totalPrice() {
        Iterator<GoodsItem> it = goodsItems.iterator();
        double total = 0;
        while (it.hasNext()) {
            total = it.next().getGoodsPrice();
        }
        return total;
    }

    @Override
    public String toString() {
        String str = "购物车：\n";
        str += "商品编号\t\t商品名字\t\t商品价格\n";
        Iterator<GoodsItem> it = goodsItems.iterator();
        while (it.hasNext()) {
            GoodsItem temp = it.next();
            str += temp.getGoodsCode();
            str += "\t\t";
            str += temp.getGoodsName();
            str += "\t\t";
            str += temp.getGoodsPrice();
            str += "\n";
        }
        return str;
    }


    //测试程序
    public static void main(String[] args) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.addGoods(1, "ps", 5500);
        shoppingCart.addGoods(2, "switch", 2500);
        shoppingCart.addGoods(3, "xbox", 3000);
        System.out.println(shoppingCart);
        System.out.println("总金额为：" + shoppingCart.totalPrice());

    }
}

class GoodsItem {
    private int goodsCode;
    private String goodsName;
    private double goodsPrice;

    public GoodsItem() {
        goodsName = "未知";
    }

    public GoodsItem(int goodsCode, String goodsName, double goodsPrice) {
        this.goodsCode = goodsCode;
        this.goodsName = goodsName;
        this.goodsPrice = goodsPrice;
    }

    public int getGoodsCode() {
        return goodsCode;
    }

    public void setGoodsCode(int goodsCode) {
        this.goodsCode = goodsCode;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public double getGoodsPrice() {
        return goodsPrice;
    }

    public void setGoodsPrice(double goodsPrice) {
        this.goodsPrice = goodsPrice;
    }
}
